package app.beit.finportal.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import app.beit.finportal.bean.MenuBean;
import app.beit.finportal.config.MenuConfig;
import app.beit.finportal.dao.MenuDao;
import app.beit.finportal.entity.Menu;
import app.beit.finportal.service.convert.MenuConvert;

public class MenuService {
  
	@Autowired
	private MenuDao menuDao;
	
	public List<MenuBean> getAllMenu() throws Exception{
		final List<MenuBean> beans = MenuConfig.getInstance().getListMenu();
		if(beans != null){
			return beans;
		}
		final List<Menu> entities = this.menuDao.getAllMenu();
		if(entities != null && entities.size() > 0){
			final List<MenuBean> lstmenuBeans = MenuConvert.entityToBean(entities);
			MenuConfig.getInstance().setListMenu(lstmenuBeans);
			return lstmenuBeans;
		}
		
		return null;
	}
	
	public List<MenuBean> getAllActiveMenu() throws Exception{
		final List<MenuBean> beans = MenuConfig.getInstance().getListActiveMenu();
		if(beans != null){
			return beans;
		}
		final List<Menu> entities = this.menuDao.getAllActiveMenu();
		if(entities != null && entities.size() > 0){
			final List<MenuBean> lstmenuBeans = MenuConvert.entityToBean(entities);
			MenuConfig.getInstance().setListActiveMenu(lstmenuBeans);
			return lstmenuBeans;
		}
		
		return null;
	}
	
	public MenuBean getAllMenu(String id) throws Exception{
		final MenuBean bean = MenuConfig.getInstance().getMenu();
		if(bean != null){
			return bean; 
		}
		final Menu entitie = this.menuDao.getById(id);
		if(entitie != null){
			final MenuBean menuBean = MenuConvert.entityToBean(entitie);
			MenuConfig.getInstance().setMenu(menuBean);
			return menuBean;
		}
		
		return null;
	}
	
	public boolean updateListMenu(final List<MenuBean> beans) throws Exception {
		final List<Menu> menus = MenuConvert.beanToEntity(beans);
		return this.menuDao.updateListMenu(menus);
	}
	
}
